<template>
  <div class='container'>
    <el-dialog :title="dialogTitle" :visible="addArticleDialog" @close="closeDialog" width="52%" @open="openDialog">
      <el-form :model="formData" label-width="120px" :rules="formRules" ref="form">
        <el-form-item label="文章标题:" prop="title" >
          <el-input v-model="formData.title" autocomplete="off" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容:" prop="articleBody">
          <quill-editor v-model="formData.articleBody" :options="editorOption"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址:" prop="videoURL"  >
          <el-input v-model="formData.videoURL" autocomplete="off" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  props: {
    addArticleDialog: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      dialogTitle: '新增文章',
      formData: {
        id: '',
        title: '',
        videoURL: '',
        articleBody: ''
      },
      formRules: {
        title: [
          { required: true, message: '请输入文章标题', tirgger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', tirgger: 'blur' }
        ]
      },
      editorOption: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote', 'code-block'], // 引用，代码块
            ['image', 'link'] // 上传图片、上传视频:'video'
          ]
        }
      }
    }
  },
  methods: {
    openDialog () {
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.form.resetFields() // clearValidate()
      })
    },
    closeDialog () {
      this.$emit('update:addArticleDialog', false)
      this.formData = {
        id: '',
        title: '',
        videoURL: '',
        articleBody: ''
      }
    },
    // 确认按钮
    async confirm () {
      this.$refs.form.validate(async boo => {
        if (boo) {
          // 提交
          if (!this.formData.id) {
            // 新增
            await add(this.formData)
          } else {
            // 修改
            await update(this.formData)
          }
          this.$message.success('操作成功')
          this.$emit('uploadData')
          this.closeDialog()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'>
/deep/ .ql-editor{
  height: 200px;
}
/deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
